package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.service.OrderService;
import com.itheima.service.ReservationService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/reservation")
public class ReservationController {
    @Reference
    private OrderService orderService;
    @Reference
    private ReservationService reservationService;

    @RequestMapping("/add")
    public Result add(@RequestBody Map map, Integer[] setmealIds) {
        try {
            Result result = orderService.orderFromBackend(map, setmealIds);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean pageBean) {
        return reservationService.findPage(pageBean);
    }

    @RequestMapping("/findById")
    public Order findById(Integer id) {
        return reservationService.findById(id);
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Map map) {
        try {
            reservationService.edit(map);
            return new Result(true,"预约修改成功!");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"预约修改失败!");
        }
    }
    @RequestMapping("/deleteById")
    public Result deleteById(Integer id) {
        try {
            reservationService.deleteById(id);
            return new Result(true,"预约删除成功!");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"预约删除失败!");
        }
    }
}
